package phoneData;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class PhoneDataCount {

    public static class PhoneDataMapper
            extends Mapper<Object, Text, Text, FlowBean> {

        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {

            String[] lineInfo = value.toString().split("\t");
            String phoneNumber = "";
            String upperFlow = "";
            String downFlow = "";

            phoneNumber = lineInfo[0].trim();
            upperFlow = lineInfo[lineInfo.length-3];
            downFlow = lineInfo[lineInfo.length-2];

            context.write(new Text(phoneNumber), new FlowBean(phoneNumber, Long.parseLong(upperFlow), Long.parseLong(downFlow)));

        }
    }

    public static class PhoneDataReducer extends Reducer<Text, FlowBean, Text, FlowBean> {

        public void reduce(Text key, Iterable<FlowBean> values, Context context )
                throws IOException, InterruptedException {

            long upperFlow = 0;
            long downFlow = 0;

            for (FlowBean bean: values) {
                upperFlow += bean.getUpFlow();
                downFlow += bean.getDownFlow();
            }

            context.write(key, new FlowBean(key.toString(), upperFlow, downFlow));


        }
    }

    public static class PhoneDataSortMapper extends Mapper<LongWritable, Text, FlowBean, NullWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {

            String line = value.toString();
            String[] fields = StringUtils.split(line, "\t");
            String phone = fields[0];
            long upStream = Long.parseLong(fields[fields.length-3]);
            long downStream = Long.parseLong(fields[fields.length-2]);

            context.write(new FlowBean(phone, upStream, downStream), NullWritable.get());
        }
    }

    public static class PhoneDataSortReducer extends Reducer<FlowBean, NullWritable, Text, FlowBean> {
        @Override
        protected void reduce(FlowBean key, Iterable<NullWritable> values, Context context)
                throws IOException, InterruptedException {

            String phone = key.getPhoneNumber();
            context.write(null,key);
        }
    }

    public static void main(String[] args) throws Exception {

        //delete the output folder before run the MR job
        FileUtils.deleteDirectory(new File("output"));
        //default word count code:
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");

        job.setJarByClass(PhoneDataCount.class);

//        job.setMapperClass(PhoneDataMapper.class);
//        job.setReducerClass(PhoneDataReducer.class);
//        job.setCombinerClass(PhoneDataReducer.class);

        job.setMapperClass(PhoneDataSortMapper.class);
        job.setReducerClass(PhoneDataSortReducer.class);
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}